package com.example.littleSpider.myapplication.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.littleSpider.myapplication.R;
import com.example.littleSpider.myapplication.base.BaseActivity;
import com.littlespider.mylibrary.util.T;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * 新增收货地址与修改收货地址的界面
 * Created by littleSpider on 2017/12/22.
 */

public class ChangeAddressActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title; //页面标题
    @BindView(R.id.rl_title)
    RelativeLayout rl_title;
    @BindView(R.id.ll_list)
    LinearLayout ll_list;
    @BindView(R.id.et_name)
    EditText et_name;
    @BindView(R.id.et_phone_number)
    EditText et_phone_number;
    @BindView(R.id.tv_choose_area)
    TextView tv_area;
    @BindView(R.id.et_address_detail)
    EditText et_address_detail;
    @BindView(R.id.et_postcode)
    EditText et_postcode;
    private PopupWindow popupWindow;

    private String areaContent;

    List<String> a = new ArrayList<>();
    private Intent intent;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_change_address;
    }

    @Override
    protected void initView() {
        intent = getIntent();
        String title = intent.getStringExtra("title");
        tv_title.setText(title);
    }

    @Override
    protected void initData() {
        String name = intent.getStringExtra("name");
        String phoneNumber = intent.getStringExtra("phoneNumber");
        String area = intent.getStringExtra("area");
        String addressDetail = intent.getStringExtra("addressDetail");
        for (int i = 0; i < 10; i++) {
            if (i == 0){
                a.add("");
            }else{
                a.add("这是" + i +"首简单的小情歌");
            }
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("")
                .setIcon(R.drawable.spider_icon)
                .setMessage("");
    }

    @OnClick(R.id.rl_back)
    public void back(){
        finish();
    }

    @OnClick(R.id.tv_choose_area)
    public void chooseArea(){
        popupWindow = new PopupWindow(ChangeAddressActivity.this);
        View view = LayoutInflater.from(this).inflate(R.layout.popup_choose_area, null);
        view.findViewById(R.id.background).setOnClickListener(myClick);
        view.findViewById(R.id.tv_cancel).setOnClickListener(myClick);
        view.findViewById(R.id.tv_sure).setOnClickListener(myClick);
        LoopView loopView = (LoopView) view.findViewById(R.id.loop_view);
        loopView.setItems(a);
        loopView.setTextSize(14f);
        loopView.setNotLoop();
        loopView.setCenterTextColor(Color.parseColor("#ff0000"));
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int i) {
                areaContent = a.get(i);
            }
        });
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3300ff99")));
        popupWindow.setWidth(MATCH_PARENT);
        popupWindow.setHeight(MATCH_PARENT);
        popupWindow.showAsDropDown(rl_title);
    }

    @OnClick(R.id.btn_sure)
    public void sure(){
        if ("".equals(getName())){
            T.show(this, "收货人姓名不能为空!");
            return;
        }
        if ("".equals(getPhoneNumber())){
            T.show(this, "手机号码不能为空!");
            return;
        }
        if ("".equals(getAddressDetail())){
            T.show(this, "详细地址不能为空!");
            return;
        }
        Toasty.success(this, "新增地址成功").show();
        finish();
    }

    @OnClick(R.id.btn_cancel)
    public void cancel(){
        T.show(this, "取消");
    }

    /**
     * 获取收件人姓名
     * @return
     */
    private String getName(){
        return et_name.getText().toString();
    }

    /**
     * 获取手机号码
     * @return
     */
    private String getPhoneNumber(){
        return et_phone_number.getText().toString();
    }

    /**
     * 获取详细地址
     * @return
     */
    private String getAddressDetail(){
        return et_address_detail.getText().toString();
    }

    /**
     * 获取邮编
     * @return
     */
    private String getPostcode(){
        return et_postcode.getText().toString();
    }

    View.OnClickListener myClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.background:
                    break;
                case R.id.tv_cancel:
                    break;
                case R.id.tv_sure:
//                    tv_area.setText("这是一个新的区域");
                    tv_area.setText(areaContent);
                    break;
                default:
                    break;
            }
            popupWindow.dismiss();
        }
    };
}
